// pages/request/index.js
const config = require("../../utils/config.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseExpress: null,
    userInfo: null,
    pickUpCode:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(app.globalData.chooseExpress)
    let str = wx.getStorageSync("userInfo")
    let userInfo = JSON.parse(str==''?null:str)
    this.setData({
      chooseExpress: app.globalData.chooseExpress,
      userInfo: userInfo
    })

  },
  addrOrder(event) {
    console.log(event.detail.value)
    let _this = this
    app.login()
      .then((res) => {
        console.log(res)
        let token = wx.getStorageSync("token")
        let data = event.detail.value
        data.token = token
        data.expressId = this.data.chooseExpress.expressId

        if (data.phone.trim() == '' || data.dormNumber.trim() == '' || data.name.trim() == '' || data.pickUpCode.trim() == '') {
          wx.showToast({
            title: '信息不能为空',
            icon: 'none'
          })
          return false;
        }

        //手机号正则
        var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
        //电话
        if (!phoneReg.test(data.phone.trim())) {
          wx.showToast({
            title: '请输入正确的手机号',
            icon: 'none'
          })
          return false;
        }

        // 宿舍号 5-109
        let scReg = /^\d+-\d{3}$/;
        if (!scReg.test(data.dormNumber.trim())) {
          wx.showToast({
            title: '宿舍格式不正确',
            icon: 'none'
          })
          return false;
        }

        wx.request({
          url: config.HOST +'/expressOrder/addExpressOrder',
          data: data,
          success(res) {
            console.log(res)
            if(res.data.status==200) {
              wx.showToast({
                title: '申请成功',
              })
              _this.setData({
                pickUpCode:''
              })
            }else {
              wx.showToast({
                title: res.data.message,
                icon: 'none'
              })
            }
          }
        })

      })
      .catch((res) => {
        console.log(res)
        wx.navigateTo({
          url: '../user/login/index',
        })
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '聚星',
      path: this.route
    }
  }
})